const express = require("express");
const path = require("path");
const bodypaser = require("body-parser");
const app = express();

require("./model/connect");

app.use(bodypaser.json());
app.use(
  bodypaser.urlencoded({
    extended: false,
  })
);

app.use(express.static(path.join(__dirname, "public")));
const admin = require("./routes/admin");

app.all("*", (req, res, next) => {
  res.header("Access-Control-Allow-Credentials", true);
  res.header(
    "Access-Control-Allow-Headers",
    "Power-By,Content-Type,X-Requested-With,X-Version,X-YFG-TOKEN"
  );
  //允许访问的方式
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  //允许所有来源访问(解决跨域的问题)
  res.header("Access-Control-Allow-Origin", "*");
  // 将控制权转到下一个中间件(移交控制权)
  next();
});

/* // 判断用户是否登录
app.use((req, res, next) => {
  let isLogin = false;
  if (isLogin) {
    console.log(isLogin);
    next();
  } else {
    res.redirect("/login.html");
  }
}); */

app.use("/admin", admin);

app.listen(9090, () => {
  console.log(`success...`);
});
